﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CM_Grid.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System
Imports Microsoft.Win32

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Customization

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '
<Assembly: CommandClass(GetType(CM_Grid))> 


#Region "Public Structures"
    
    '// --------------------------------------------------------------------- '
'// common settings for model and paper
    Public Structure CRJSTG_CMN
        Dim IN_mrk As Boolean ' show crosses
        Dim IN_txt As Boolean ' values are shown on top of cross
        Dim IN_dst As Double ' distance at which crosses are placed
        Dim IN_txh As Double ' height of text
        Dim IN_csz As Double ' cross halflen

        Dim BR_mrk As Boolean ' show side values
        Dim BR_txt As Boolean ' side values
        Dim BR_dst As Double ' distance at which crosses are placed
        Dim BR_txh As Double ' height of text
        Dim BR_csz As Double ' side halflen

        Dim iMaxObj As Integer '// maximum number of objects
    End Structure
    
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// defines the settings for creating a grid
    Public Structure CRJSTG
        Dim M As CRJSTG_CMN
        Dim P As CRJSTG_CMN
    End Structure
    '// ===================================================================== '



#End Region


'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '
Public Class CM_Grid
	Inherits CModule


    '// --------------------------------------------------------------------- '
    '// DATA

    #Region "Public Constants" 
        '// name as displayed in various places
	    Public Const sAPPNAME As String = "Grid"
        '// version as displayed in various places
	    Public Const sAPPVER As String = "1.0.0.0"
        '// short description
	    Public sAPPTIP As String

        '// Command. Brings up the settings dialog
        Public Const sCMD_SETTINGS As String = "TNIK_GRID_STG"
        '// Command. generates grid in model
        Public Const sCMD_MODEL As String = "TNIK_GRID_M"
        '// Command. generates grid in page
        Public Const sCMD_PAPER As String = "TNIK_GRID_P"
        '// Command. asks for options and generates grid in model
        Public Const sCMD_MODELOPTS As String = "TNIK_GRID_MO"
        '// Command. asks for options and generates grid in page
        Public Const sCMD_PAPEROPTS As String = "TNIK_GRID_PO"
    #End Region

    '// this is where the settings are stored durring run-time
    Public shared Stgs As CRJSTG

    '// ===================================================================== '


#Region "Private general purpose Functions"


    '// --------------------------------------------------------------------- '
	'// This function saves current settings
	Private Shared Sub SaveSett()

		CSett.SaveStg(sAPPNAME, "Stgs.M.IN_mrk", Stgs.M.IN_mrk)
		CSett.SaveStg(sAPPNAME, "Stgs.M.IN_txt", Stgs.M.IN_txt)
		CSett.SaveStg(sAPPNAME, "Stgs.M.IN_dst", Stgs.M.IN_dst)
		CSett.SaveStg(sAPPNAME, "Stgs.M.IN_txh", Stgs.M.IN_txh)
		CSett.SaveStg(sAPPNAME, "Stgs.M.IN_csz", Stgs.M.IN_csz)
		CSett.SaveStg(sAPPNAME, "Stgs.M.BR_mrk", Stgs.M.BR_mrk)
		CSett.SaveStg(sAPPNAME, "Stgs.M.BR_txt", Stgs.M.BR_txt)
		CSett.SaveStg(sAPPNAME, "Stgs.M.BR_dst", Stgs.M.BR_dst)
		CSett.SaveStg(sAPPNAME, "Stgs.M.BR_txh", Stgs.M.BR_txh)
		CSett.SaveStg(sAPPNAME, "Stgs.M.BR_csz", Stgs.M.BR_csz)

		CSett.SaveStg(sAPPNAME, "Stgs.M.iMaxObj", Stgs.M.iMaxObj)

		CSett.SaveStg(sAPPNAME, "Stgs.P.IN_mrk", Stgs.P.IN_mrk)
		CSett.SaveStg(sAPPNAME, "Stgs.P.IN_txt", Stgs.P.IN_txt)
		CSett.SaveStg(sAPPNAME, "Stgs.P.IN_dst", Stgs.P.IN_dst)
		CSett.SaveStg(sAPPNAME, "Stgs.P.IN_txh", Stgs.P.IN_txh)
		CSett.SaveStg(sAPPNAME, "Stgs.P.IN_csz", Stgs.P.IN_csz)
		CSett.SaveStg(sAPPNAME, "Stgs.P.BR_mrk", Stgs.P.BR_mrk)
		CSett.SaveStg(sAPPNAME, "Stgs.P.BR_txt", Stgs.P.BR_txt)
		CSett.SaveStg(sAPPNAME, "Stgs.P.BR_dst", Stgs.P.BR_dst)
		CSett.SaveStg(sAPPNAME, "Stgs.P.BR_txh", Stgs.P.BR_txh)
		CSett.SaveStg(sAPPNAME, "Stgs.P.BR_csz", Stgs.P.BR_csz)

		CSett.SaveStg(sAPPNAME, "Stgs.P.iMaxObj", Stgs.P.iMaxObj)

        GC.Collect()
        GC.WaitForPendingFinalizers()
	End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
	'// This function loads current settings
	Private Shared Sub GetSett()

		Stgs.M.IN_mrk = CSett.GetBolStg(sAPPNAME, "Stgs.M.IN_mrk", True)
		Stgs.M.IN_txt = CSett.GetBolStg(sAPPNAME, "Stgs.M.IN_txt", True)
		Stgs.M.IN_dst = CSett.GetDblStg(sAPPNAME, "Stgs.M.IN_dst", 100)
		Stgs.M.IN_txh = CSett.GetDblStg(sAPPNAME, "Stgs.M.IN_txh", 1.2)
		Stgs.M.IN_csz = CSett.GetDblStg(sAPPNAME, "Stgs.M.IN_csz", 10)

		Stgs.M.BR_mrk = CSett.GetBolStg(sAPPNAME, "Stgs.M.BR_mrk", True)
		Stgs.M.BR_txt = CSett.GetBolStg(sAPPNAME, "Stgs.M.BR_txt", True)
		Stgs.M.BR_dst = CSett.GetDblStg(sAPPNAME, "Stgs.M.BR_dst", 100)
		Stgs.M.BR_txh = CSett.GetDblStg(sAPPNAME, "Stgs.M.BR_txh", 1.2)
		Stgs.M.BR_csz = CSett.GetDblStg(sAPPNAME, "Stgs.M.BR_csz", 10)

		Stgs.M.iMaxObj = CSett.GetIntStg(sAPPNAME, "Stgs.M.iMaxObj", 500)


		Stgs.P.IN_mrk = CSett.GetBolStg(sAPPNAME, "Stgs.P.IN_mrk", True)
		Stgs.P.IN_txt = CSett.GetBolStg(sAPPNAME, "Stgs.P.IN_txt", True)
		Stgs.P.IN_dst = CSett.GetDblStg(sAPPNAME, "Stgs.P.IN_dst", 100)
		Stgs.P.IN_txh = CSett.GetDblStg(sAPPNAME, "Stgs.P.IN_txh", 2)
		Stgs.P.IN_csz = CSett.GetDblStg(sAPPNAME, "Stgs.P.IN_csz", 10)

		Stgs.P.BR_mrk = CSett.GetBolStg(sAPPNAME, "Stgs.P.BR_mrk", True)
		Stgs.P.BR_txt = CSett.GetBolStg(sAPPNAME, "Stgs.P.BR_txt", True)
		Stgs.P.BR_dst = CSett.GetDblStg(sAPPNAME, "Stgs.P.BR_dst", 100)
		Stgs.P.BR_txh = CSett.GetDblStg(sAPPNAME, "Stgs.P.BR_txh", 2)
		Stgs.P.BR_csz = CSett.GetDblStg(sAPPNAME, "Stgs.P.BR_csz", 10)

		Stgs.P.iMaxObj = CSett.GetIntStg(sAPPNAME, "Stgs.P.iMaxObj", 500)

        GC.Collect()
        GC.WaitForPendingFinalizers()
	End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
	'// This function shows current settings
	Private Shared Function PrintStg(ByRef lcStg As CRJSTG) As String
		Return (vbCrLf & CMain._T(TX.CSTG) & ": " & vbCrLf & _
        _
        CMain._T(TX.MODEL) & ":" & vbCrLf & _
		  CMain._T(TX.CROSS) & " = " & CStr(lcStg.M.IN_mrk) & vbCrLf & _
		  vbTab & CMain._T(TX.VALUES)  & " = " & CStr(lcStg.M.IN_txt) & vbCrLf & _
		  vbTab & CMain._T(TX.HTEXT)  & " = " & Format(lcStg.M.IN_txh, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.HLINE)  & " = " & Format(lcStg.M.IN_csz, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.SPAN) & " = " & Format(lcStg.M.IN_dst, "0.000") & vbCrLf & _
		  CMain._T(TX.BORDER) & " = " & CStr(lcStg.M.BR_mrk) & vbCrLf & _
		  vbTab & CMain._T(TX.VALUES) &  " = " & CStr(lcStg.M.BR_txt) & vbCrLf & _
		  vbTab & CMain._T(TX.HTEXT) &  " = " & Format(lcStg.M.BR_txh, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.HLINE) &  " = " & Format(lcStg.M.BR_csz, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.SPAN) & " = " & Format(lcStg.M.BR_dst, "0.000") & vbCrLf & vbCrLf & _
		  vbCrLf & vbCrLf & _
		  vbTab & CMain._T(TX.MAXGRIDOBJ) & " = " & lcStg.M.iMaxObj.ToString & vbCrLf & vbCrLf & _
		  vbCrLf & vbCrLf & _
		  vbCrLf & vbCrLf & _
        CMain._T(TX.PAPER) & ":" & vbCrLf & _
		  CMain._T(TX.CROSS) & " = " & CStr(lcStg.P.IN_mrk) & vbCrLf & _
		  vbTab & CMain._T(TX.VALUES)  & " = " & CStr(lcStg.P.IN_txt) & vbCrLf & _
		  vbTab & CMain._T(TX.HTEXT)  & " = " & Format(lcStg.P.IN_txh, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.HLINE)  & " = " & Format(lcStg.P.IN_csz, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.SPAN) & " = " & Format(lcStg.P.IN_dst, "0.000") & vbCrLf & _
		  CMain._T(TX.BORDER) & " = " & CStr(lcStg.P.BR_mrk) & vbCrLf & _
		  vbTab & CMain._T(TX.VALUES) &  " = " & CStr(lcStg.P.BR_txt) & vbCrLf & _
		  vbTab & CMain._T(TX.HTEXT) &  " = " & Format(lcStg.P.BR_txh, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.HLINE) &  " = " & Format(lcStg.P.BR_csz, "0.000") & vbCrLf & _
		  vbTab & CMain._T(TX.SPAN) & " = " & Format(lcStg.P.BR_dst, "0.000") & vbCrLf & vbCrLf & _
		  vbCrLf & vbCrLf & _
		  vbTab & CMain._T(TX.MAXGRIDOBJ) & " = " & lcStg.P.iMaxObj.ToString & vbCrLf & vbCrLf & _
		  vbCrLf & vbCrLf & _
		  vbCrLf & vbCrLf)

	End Function
    '// ===================================================================== '


#End Region


#Region "Common interface" ' -----------------------

    '// --------------------------------------------------------------------- '
	'// a new instance is created
	Public Sub New()
		Try
            sAPPTIP = CMain._T(TX.GRID_INFO)
			Return
		Catch ex As System.Exception
			CMain.DebugException(ex)
			Return
		End Try
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// the module is asked to terminate
	Public Overrides Function Terminate() As Boolean
		Try
			SaveSett()
			Return True
		Catch ex As System.Exception
			CMain.DebugException(ex)
			Return False
		End Try
	End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// the module is asked to tell it's name
	Public Shared Function GetName() As String
		Return sAPPNAME
	End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// the module is asked to tell about itself
	Public Shared Sub GetInfos(ByRef sName As String, ByRef sVer As String, ByRef sTip As String)
		sName = sAPPNAME
		sVer = sAPPVER
		sTip = CMain._T(TX.GRID_INFO)
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// the module is asked to tell about itself
	Public Shared Function GetDescription() As String
        Dim sOut As String = sAPPNAME
        Try
            Dim cIni As New IniWork(CApp.sAppPath & CMain.APPSTG)
            If cIni.HasErrors = True Then Exit Try
            sOut = cIni.Read(sAPPNAME, CMain.DESCRKEY & _
                             CMain.eAppLang.ToString,sAPPNAME)
        Catch ex As System.Exception
            CMain.DebugException(ex)
        End Try
        Return sOut
	End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// the module is informed about application start-up
	Public Shared Sub DoPrereq()
		Try
			' this function is called at application start-up in order to 
			' allow applications to do any prerequisites
			' there is no instance of this class at this time
			GetSett()
		Catch ex As System.Exception
			CMain.DebugException(ex)
		End Try
		Return
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// create the user interface for this module
	Public Shared Sub DoCUI( _
	 ByRef pcs As CustomizationSection, _
	 ByRef mg As MacroGroup)
		' this function is called when a CUI is builded in order to
		' allow any application specifics
		Try
        #If HARDCORE_CUI
            If CApp.cuiCl Is Nothing then Exit Try
            CApp.cuiCl.AddMacro(sCMD_MODEL,"GRID_Model")
            CApp.cuiCl.AddMacro(sCMD_PAPER,"GRID_Paper")
            CApp.cuiCl.AddMacro(sCMD_MODELOPTS,"GRID_ModelO")
            CApp.cuiCl.AddMacro(sCMD_PAPEROPTS,"GRID_Papero")
            CApp.cuiCl.AddMacro(sCMD_SETTINGS,"GRID_Settings")

            CApp.cuiCl.AddToolbar( _
                sAPPNAME, _
                Application.MainWindow.Location.X, _
                100 + MIDS.GRID * 30)
            CApp.cuiCl.AddToolbarBtn(CMain._T(tx.MODEL),sCMD_MODEL)
            CApp.cuiCl.AddToolbarBtn(CMain._T(tx.PAPER),sCMD_PAPER)
            CApp.cuiCl.AddToolbarBtn(CMain._T(tx.MODEL),sCMD_MODELOPTS)
            CApp.cuiCl.AddToolbarBtn(CMain._T(tx.PAPER),sCMD_PAPEROPTS)
            CApp.cuiCl.AddToolbarSep()
            CApp.cuiCl.AddToolbarBtn(CMain._T(tx.SETTINGS),sCMD_SETTINGS)

        #Else

			Dim mm1 As MenuMacro = Nothing
			mm1 = New MenuMacro(mg, sCMD_MODEL, "^C^C" & sCMD_MODEL, "ID_" & sCMD_MODEL)
			mm1 = New MenuMacro(mg, sCMD_PAPER, "^C^C" & sCMD_PAPER, "ID_" & sCMD_PAPER)
			mm1 = New MenuMacro(mg, sCMD_MODELOPTS, "^C^C" & sCMD_MODELOPTS, "ID_" & sCMD_MODELOPTS)
			mm1 = New MenuMacro(mg, sCMD_PAPEROPTS, "^C^C" & sCMD_PAPEROPTS, "ID_" & sCMD_PAPEROPTS)
			mm1 = New MenuMacro(mg, sCMD_SETTINGS, "^C^C" & sCMD_SETTINGS, "ID_" & sCMD_SETTINGS)


			Dim tbBtn As ToolbarButton = Nothing
			Dim newTb As Toolbar = New Toolbar(sAPPNAME, pcs.MenuGroup)
			newTb.ElementID = "EID_TNIK_Grid"
			newTb.ToolbarOrient = ToolbarOrient.floating
			newTb.ToolbarVisible = ToolbarVisible.show
            newTb.XCoordinate = Application.MainWindow.Location.X
            newTb.YCoordinate = 100 + MIDS.GRID * 30

			'Dim tbCtrl As ToolbarControl = New ToolbarControl(ControlType.NamedViewControl, newTb, -1)


			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.MODEL)
			tbBtn.MacroID = "ID_" & sCMD_MODEL
			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PAPER)
			tbBtn.MacroID = "ID_" & sCMD_PAPER
			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.MODEL) & " + " & CMain._T(tx.OPTIONS)
			tbBtn.MacroID = "ID_" & sCMD_MODELOPTS
			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PAPER) & " + " & CMain._T(tx.OPTIONS)
			tbBtn.MacroID = "ID_" & sCMD_PAPEROPTS
			tbBtn = New ToolbarButton(newTb, -1) ' separator
			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.SETTINGS)
			tbBtn.MacroID = "ID_" & sCMD_SETTINGS

        #endif
		Catch ex As System.Exception
			CMain.DebugException(ex)
		End Try
	End Sub
    '// ===================================================================== '
#End Region

#Region "Internal base work"

    '// --------------------------------------------------------------------- '
	'// create one element in paper space, inside
    Private Shared Function One_In_Paper_In( _
        ByRef acTrans As Transaction, _
        ByRef acBlk As BlockTableRecord, _
        ByVal dN As Double, _
        ByVal dE As Double, _
        ByRef lcStg As CRJSTG_CMN) As Boolean
        



        Return True
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// create one element in model space, inside
    Private Shared Function One_In_Model_In( _
        ByRef acTrans As Transaction, _
        ByRef acBlk As BlockTableRecord, _
        ByVal dN As Double, _
        ByVal dE As Double, _
        ByRef lcStg As CRJSTG_CMN) As Boolean
        
        Dim acLine As Line = Nothing
        Dim acText As DBText = Nothing
        Dim bRet As Boolean = False

        Try
            If (lcStg.IN_txt) Then

                acText = New DBText()
                acText.SetDatabaseDefaults()
                acText.TextString = dN.ToString("0")
				acText.Position = New Point3d( _
                    dE + lcStg.IN_txh / 2, _
                    dN + lcStg.IN_txh / 2, _
                    0)
				acText.Height = Stgs.M.IN_txh
                acBlk.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText,True)
                acText = Nothing

                acText = New DBText()
                acText.SetDatabaseDefaults()
                acText.TextString = dE.ToString("0")
				acText.Position = New Point3d( _
                    dE - lcStg.IN_txh / 2, _
                    dN + lcStg.IN_txh / 2, _
                    0)
				acText.Height = Stgs.M.IN_txh
                acText.Rotation = System.Math.PI / 2
                acBlk.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText,True)
                acText = Nothing

            End If
            If (lcStg.IN_mrk) Then
                acLine = New Line()
                acLine.SetDatabaseDefaults()
                acLine.StartPoint = New Point3d( _
                                dE - Stgs.M.IN_csz, dN, 0)
                acLine.EndPoint = New Point3d( _
                                dE + Stgs.M.IN_csz, dN, 0)
                acBlk.AppendEntity(acLine)
                acTrans.AddNewlyCreatedDBObject(acLine,True)
                acLine = Nothing

                acLine = New Line()
                acLine.SetDatabaseDefaults()
                acLine.StartPoint = New Point3d( _
                                dE, dN - Stgs.M.IN_csz, 0)
                acLine.EndPoint = New Point3d( _
                                dE, dN + Stgs.M.IN_csz, 0)
                acBlk.AppendEntity(acLine)
                acTrans.AddNewlyCreatedDBObject(acLine,True)
                acLine = Nothing

            End If

            bRet = True
        Catch ex As System.Exception
            CMain.DebugException(ex)
            If acLine isnot Nothing Then
                acLine.Dispose
                acLine = Nothing
            End If
            If acText isnot Nothing Then
                acText.Dispose
                acText = Nothing
            End If
        End Try

        Return bRet
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// create one element in paper space, on border
    Private Shared Function One_In_Paper_Brd() As Boolean
        





        Return True
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// create one element in model space, on border, in north direction
    Private Shared Function One_In_Model_Brd_N( _
        ByRef acTrans As Transaction, _
        ByRef acBlk As BlockTableRecord, _
        ByVal dN As Double, _
        ByVal dE_Left As Double, _
        ByVal dE_Right As Double, _
        ByRef lcStg As CRJSTG_CMN) As Boolean
        
        Dim acLine As Line = Nothing
        Dim acText As DBText = Nothing
        Dim bRet As Boolean = False

        Try
            If (lcStg.BR_txt) Then

                acText = New DBText()
                acText.SetDatabaseDefaults()
                acText.TextString = dN.ToString("0")
				acText.Position = New Point3d( _
                    dE_Left + 3 * lcStg.BR_txh / 2, _
                    dN + lcStg.BR_txh / 2, _
                    0)
				acText.Height = Stgs.M.BR_txh
                acText.Rotation = System.Math.PI / 2
                acBlk.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText,True)
                acText = Nothing

                acText = New DBText()
                acText.SetDatabaseDefaults()
                acText.TextString = dN.ToString("0")
				acText.Position = New Point3d( _
                    dE_Right - lcStg.BR_txh / 2, _
                    dN + lcStg.BR_txh / 2, _
                    0)
				acText.Height = Stgs.M.BR_txh
                acText.Rotation = System.Math.PI / 2
                acBlk.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText,True)
                acText = Nothing

            End If
            If (lcStg.BR_mrk) Then
                acLine = New Line()
                acLine.SetDatabaseDefaults()
                acLine.StartPoint = New Point3d( _
                                dE_Right, dN, 0)
                acLine.EndPoint = New Point3d( _
                                dE_Right - Stgs.M.BR_csz, dN, 0)
                acBlk.AppendEntity(acLine)
                acTrans.AddNewlyCreatedDBObject(acLine,True)
                acLine = Nothing

                acLine = New Line()
                acLine.SetDatabaseDefaults()
                acLine.StartPoint = New Point3d( _
                                dE_Left, dN, 0)
                acLine.EndPoint = New Point3d( _
                                dE_Left + Stgs.M.BR_csz, dN, 0)
                acBlk.AppendEntity(acLine)
                acTrans.AddNewlyCreatedDBObject(acLine,True)
                acLine = Nothing

            End If

            bRet = True
        Catch ex As System.Exception
            CMain.DebugException(ex)
            If acLine isnot Nothing Then
                acLine.Dispose
                acLine = Nothing
            End If
            If acText isnot Nothing Then
                acText.Dispose
                acText = Nothing
            End If
        End Try

        Return bRet
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	'// create one element in model space, on border, in east direction
    Private Shared Function One_In_Model_Brd_E( _
        ByRef acTrans As Transaction, _
        ByRef acBlk As BlockTableRecord, _
        ByVal dE As Double, _
        ByVal dN_Top As Double, _
        ByVal dN_Btm As Double, _
        ByRef lcStg As CRJSTG_CMN) As Boolean
        
        Dim acLine As Line = Nothing
        Dim acText As DBText = Nothing
        Dim bRet As Boolean = False

        Try
            If (lcStg.BR_txt) Then

                acText = New DBText()
                acText.SetDatabaseDefaults()
                acText.TextString = dE.ToString("0")
				acText.Position = New Point3d( _
                    dE + lcStg.BR_txh / 2, _
                    dN_Btm + lcStg.BR_txh / 2, _
                    0)
				acText.Height = Stgs.M.BR_txh
                acText.Rotation = System.Math.PI / 2
                acBlk.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText,True)
                acText = Nothing

                acText = New DBText()
                acText.SetDatabaseDefaults()
                acText.TextString = dE.ToString("0")
				acText.Position = New Point3d( _
                    dE + lcStg.BR_txh / 2, _
                    dN_Top - 3 * lcStg.BR_txh / 2, _
                    0)
				acText.Height = Stgs.M.BR_txh
                acText.Rotation = System.Math.PI / 2
                acBlk.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText,True)
                acText = Nothing

            End If
            If (lcStg.BR_mrk) Then
                acLine = New Line()
                acLine.SetDatabaseDefaults()
                acLine.StartPoint = New Point3d( _
                                dE, dN_Btm, 0)
                acLine.EndPoint = New Point3d( _
                                dE, dN_Btm + Stgs.M.BR_csz, 0)
                acBlk.AppendEntity(acLine)
                acTrans.AddNewlyCreatedDBObject(acLine,True)
                acLine = Nothing

                acLine = New Line()
                acLine.SetDatabaseDefaults()
                acLine.StartPoint = New Point3d( _
                                dE, dN_Top, 0)
                acLine.EndPoint = New Point3d( _
                                dE, dN_Top - Stgs.M.BR_csz, 0)
                acBlk.AppendEntity(acLine)
                acTrans.AddNewlyCreatedDBObject(acLine,True)
                acLine = Nothing

            End If

            bRet = True
        Catch ex As System.Exception
            CMain.DebugException(ex)
            If acLine isnot Nothing Then
                acLine.Dispose
                acLine = Nothing
            End If
            If acText isnot Nothing Then
                acText.Dispose
                acText = Nothing
            End If
        End Try

        Return bRet
    End Function
    '// ===================================================================== '



    '// --------------------------------------------------------------------- '
	'// create the user interface for this module
    Public Shared Function Do_Model_Grid(ByRef lcStg As CRJSTG) As Boolean

        Dim PctCnt As Long
        Dim bRet As Boolean = False
        Dim dDifN As Double, dDifE As Double
        Dim dCrtN As Double, dCrtE As Double
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acLine As Line
        Dim acText As DBText
        If (acDoc Is Nothing) Then
            Return False
        End If


        Using acLock As DocumentLock = acDoc.LockDocument()
        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
			    If acDoc.Database.TileMode = FALSE Then
				    acDoc.Database.TileMode = TRUE
			    End If

                '// --------------------------------------------------
                '// Get Model space (is just a block)
                Dim acBlkModel As BlockTableRecord = DirectCast( _
                    acTrans.GetObject( _
                        acDoc.Database.CurrentSpaceId, _
                        OpenMode.ForWrite),  _
                    BlockTableRecord)
                
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// Have the user select the area
                Dim ppo As PromptPointOptions
                Dim ppr As PromptPointResult
                Dim pco As PromptCornerOptions
                Dim Rct As FSK_RECT
                ppo = New PromptPointOptions(vbLf & CMain._T(TX.FPOINT) & ":")
                ppr = acDoc.Editor.GetPoint(ppo)
                If ppr.Status <> PromptStatus.OK Then Exit Try
                Rct.N_1 = ppr.Value.Y
                Rct.E_1 = ppr.Value.X

                pco = New PromptCornerOptions(vbLf & CMain._T(TX.SPOINT) & ":", ppr.Value)
                pco.UseDashedLine = True
                Do
                    ppr = acDoc.Editor.GetCorner(pco)
                    If ppr.Status <> PromptStatus.OK Then Exit Try

                    If Math.Abs(Rct.N_1 - ppr.Value.Y) > 0.001 Then
                        If Math.Abs(Rct.E_1 - ppr.Value.X) > 0.001 Then
                            If Rct.N_1 > ppr.Value.Y Then
                                Rct.N_2 = Rct.N_1
                                Rct.N_1 = ppr.Value.Y
                            Else
                                Rct.N_2 = ppr.Value.Y
                            End If
                            If Rct.E_1 > ppr.Value.X Then
                                Rct.E_2 = Rct.E_1
                                Rct.E_1 = ppr.Value.X
                            Else
                                Rct.E_2 = ppr.Value.X
                            End If
                            Exit Do
                        End If
                    End If
                    CMain.AlertUser(TX.ERR_PTCLOSE)
                Loop
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// compute base points for inside

			    Dim CrtNB_IN as Double = _
			        ((CLng(Rct.N_1 + lcStg.M.IN_csz) \ CLng(lcStg.M.IN_dst)) + 1) * _
			            lcStg.M.IN_dst
			    Dim CrtEB_IN as Double = _
			        ((CLng(Rct.E_1 + lcStg.M.IN_csz) \ CLng(lcStg.M.IN_dst)) + 1) * _
			            lcStg.M.IN_dst
                If (lcStg.M.IN_mrk Or lcStg.M.IN_txt) Then
                    '// these points have the property that are first points
                    '// visible to the left
                    dDifE = Rct.E_2 - CrtEB_IN
                    dDifN = Rct.N_2 - CrtNB_IN
                    If (dDifN < 0) or (dDifE < 0)  Then
                        '// No point is visible
                        CMain.AlertUser(TX.GRID_ENONE)
                        Exit Try
                    End If
                    '// compute number of points; see if they are too manny
                    PctCnt = CLng((dDifN / lcStg.M.IN_dst) * (dDifE / lcStg.M.IN_dst))
                    If (PctCnt > lcStg.M.iMaxObj)
				        CMain.AlertUser( _
                            String.Format(CMain._T(TX.GRID_ERROVRF), _
                                          PctCnt, Stgs.M.iMaxObj))
				        CMain.InformUser( _
				            PrintStg(lcStg))
                        Exit Try
                    End If
                End If
                PctCnt = 0
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// loop inside
                dCrtE = CrtEB_IN
                Do
                    dCrtN = CrtNB_IN
                    Do
                        If Stgs.M.IN_mrk Then
                            '// we have position where we have to place a marker at
                            acLine = New Line( _
                                New Point3d(dCrtE - Stgs.M.IN_csz / 2, dCrtN, 0), _
                                New Point3d(dCrtE + Stgs.M.IN_csz / 2, dCrtN, 0))
                            acLine.SetDatabaseDefaults(acDoc.Database)
						    acBlkModel.AppendEntity(acLine)
						    acTrans.AddNewlyCreatedDBObject(acLine, True)

                            acLine = New Line( _
                                New Point3d(dCrtE, dCrtN - Stgs.M.IN_csz / 2, 0), _
                                New Point3d(dCrtE, dCrtN + Stgs.M.IN_csz / 2, 0))
                            acLine.SetDatabaseDefaults(acDoc.Database)
						    acBlkModel.AppendEntity(acLine)
						    acTrans.AddNewlyCreatedDBObject(acLine, True)
                        End If
                        
                        If Stgs.M.IN_txt Then

                            '// Horizontal; the north
                            acText = New DBText()
							acText.SetDatabaseDefaults()
							acText.Position = New Point3d( _
                                dCrtE + Stgs.M.IN_txh / 2, _
                                dCrtN + Stgs.M.IN_txh / 2, _
                                0)
							acText.Height = Stgs.M.IN_txh
							acText.TextString = Format(dCrtN, "0")
							acBlkModel.AppendEntity(acText)
							acTrans.AddNewlyCreatedDBObject(acText, True)

                            '// Vertical; the east
                            acText = New DBText()
							acText.SetDatabaseDefaults()
							acText.Position = New Point3d( _
                                dCrtE - Stgs.M.IN_txh / 2, _
                                dCrtN + Stgs.M.IN_txh / 2, _
                                0)
							acText.Height = Stgs.M.IN_txh
							acText.TextString = Format(dCrtE, "0")
                            acText.Rotation = System.Math.PI / 2
							acBlkModel.AppendEntity(acText)
							acTrans.AddNewlyCreatedDBObject(acText, True)

                        End If
                        PctCnt = PctCnt + 1
                        dCrtN = dCrtN + Stgs.M.IN_dst
                    Loop Until dCrtN > (Rct.N_2 - Stgs.M.IN_csz)
                    dCrtE = dCrtE + Stgs.M.IN_dst
                Loop Until dCrtE > (Rct.E_2 - Stgs.M.IN_csz)
                CMain.InformUser(String.Format(CMain._T(TX.GRIDRSLT_I), PctCnt))
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// compute base points for outside
			    Dim CrtNB_BR as Double = _
			        ((CLng(Rct.N_1 + lcStg.M.IN_csz) \ CLng(lcStg.M.BR_dst)) + 1) * _
			            lcStg.M.BR_dst
			    Dim CrtEB_BR as Double = _
			        ((CLng(Rct.E_1 + lcStg.M.IN_csz) \ CLng(lcStg.M.BR_dst)) + 1) * _
			            lcStg.M.BR_dst
                If (lcStg.M.BR_mrk Or lcStg.M.BR_txt) Then
                    '// these points have the property that are first points
                    '// visible to the left
                    dDifE = Rct.E_2 - CrtEB_IN
                    dDifN = Rct.N_2 - CrtNB_IN
                    If (dDifN < 0) or (dDifE < 0)  Then
                        '// No point is visible
                        CMain.AlertUser(TX.GRID_ENONE)
                        Exit Try
                    End If
                    '// compute number of points; see if they are too manny
                    PctCnt = CLng((dDifN / lcStg.M.BR_dst) * (dDifE / lcStg.M.BR_dst))
                    If (PctCnt > lcStg.M.iMaxObj)
				        CMain.AlertUser( _
                            String.Format(CMain._T(TX.GRID_ERROVRF), _
                                          PctCnt, Stgs.M.iMaxObj))
				        CMain.InformUser( _
				            PrintStg(lcStg))
                        Exit Try
                    End If
                End If
                PctCnt = 0
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// loop on border
                dCrtE = CrtEB_BR
                Do
                    If Stgs.M.BR_mrk Then
                        '// we have position where we have to place a marker at
                        acLine = New Line( _
                            New Point3d(dCrtE, Rct.N_1, 0), _
                            New Point3d(dCrtE, Rct.N_1 + Stgs.M.BR_csz / 2, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                        acLine = New Line( _
                            New Point3d(dCrtE, Rct.N_2, 0), _
                            New Point3d(dCrtE, Rct.N_2 - Stgs.M.BR_csz / 2, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                    End If

                    If Stgs.M.BR_txt Then
                        '// top
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                            dCrtE + Stgs.M.IN_txh / 2, _
                            Rct.N_1 + Stgs.M.IN_txh / 2, _
                            0)
						acText.Height = Stgs.M.IN_txh
						acText.TextString = Format(dCrtN, "0")
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)

                        '// bottom
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                            dCrtE + Stgs.M.IN_txh / 2, _
                            Rct.N_2 - 3 * Stgs.M.IN_txh / 2, _
                            0)
						acText.Height = Stgs.M.IN_txh
						acText.TextString = Format(dCrtN, "0")
						acText.SetDatabaseDefaults()
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)

                    End If
                    PctCnt = PctCnt + 1
                    dCrtE = dCrtE + Stgs.M.IN_dst
                Loop Until dCrtE > Rct.E_2 - Stgs.M.BR_csz

                dCrtN = CrtNB_BR
                Do
                    If Stgs.M.BR_mrk Then
                        '// we have position where we have to place a marker at
                        acLine = New Line( _
                            New Point3d(Rct.E_1, dCrtN, 0), _
                            New Point3d(Rct.E_1 + Stgs.M.BR_csz / 2, dCrtN, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                        acLine = New Line( _
                            New Point3d(Rct.E_2, dCrtN, 0), _
                            New Point3d(Rct.E_2 - Stgs.M.BR_csz / 2, dCrtN, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                    End If

                    If Stgs.M.BR_txt Then
                        '// left
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                            Rct.E_1 + 3*Stgs.M.IN_txh / 2, _
                            dCrtN + Stgs.M.IN_txh / 2, _
                            0)
						acText.Height = Stgs.M.IN_txh
						acText.TextString = Format(dCrtN, "0")
						acText.SetDatabaseDefaults()
                        acText.Rotation = System.Math.PI / 2
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)

                        '// right
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                            Rct.E_2 - Stgs.M.IN_txh / 2, _
                            dCrtN + Stgs.M.IN_txh / 2, _
                            0)
						acText.Height = Stgs.M.IN_txh
						acText.TextString = Format(dCrtN, "0")
						acText.SetDatabaseDefaults()
                        acText.Rotation = System.Math.PI / 2
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)
                    End If

                    PctCnt = PctCnt + 1
                    dCrtN = dCrtN + Stgs.M.IN_dst
                Loop Until dCrtN > Rct.N_2 - Stgs.M.BR_csz
                CMain.InformUser(String.Format(CMain._T(TX.GRIDRSLT_B), PctCnt))
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                acTrans.Commit()
                bRet = True
                Exit Try
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
            End Try
        End Using '// acTrans
        End Using '// acLock

        Return bRet


    End Function
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
	'// create the user interface for this module
    Public Shared Function Do_Paper_Grid(ByRef lcStg As CRJSTG) As Boolean

        Dim bRet As Boolean = False
        Dim scl As double
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        ' coordinates of lower left and upper right corners of viewport in WCS
        Dim Pt_MinW As Point3d
        Dim Pt_MaxW As Point3d
        Dim acLine As Line
        Dim acText As DBText
        Dim PtSt_W As Point3d '// start point in WCS
        Dim PtSt_P As Point3d '// start point in paper
        Dim dCrtE_P As Double '// paper; east used in iterations
        Dim dCrtN_P As Double '// paper; north used in iterations
        Dim dCrtE_M As Double '// model; east used in iterations
        Dim dCrtN_M As Double '// model; north used in iterations
        Dim PctCnt As Integer '// number of points generated
        If (acDoc Is Nothing) Then
            Return False
        End If


        Using acLock As DocumentLock = acDoc.LockDocument()
        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
			    If acDoc.Database.TileMode = TRUE Then
				    acDoc.Database.TileMode = FALSE
			    End If

                '// --------------------------------------------------
                '// Get Paper space (is just a block)
                Dim acBlkModel As BlockTableRecord = DirectCast( _
                    acTrans.GetObject( _
                        acDoc.Database.CurrentSpaceId, _
                        OpenMode.ForWrite),  _
                    BlockTableRecord)
                
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// Have the user select the viewport

    		    Dim peo As PromptEntityOptions = Nothing
                Dim acVp As Viewport = Nothing
        
    	        '' Get the PickFirst selection set
    	        Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.SelectImplied()
    	        '' If the prompt status is OK, objects were selected before
    	        '' the command was started
    	        If acSSPrompt.Status <> PromptStatus.OK Then
                    HaveUserSelect:
                    Dim pso As  New PromptSelectionOptions()
                    pso.SinglePickInSpace = True
                    pso.MessageForAdding = vbLf & CMain._T(TX.SELVP) & ": "
                    acSSPrompt = _
                        acDoc.Editor.GetSelection(pso)
    	            If acSSPrompt.Status <> PromptStatus.OK Then Exit Try
                End if
		        Dim acSSet As SelectionSet = acSSPrompt.Value
	            For Each sobj As SelectedObject In acSSet
                    Dim TheObj As DBObject
                    TheObj = acTrans.GetObject(sobj.ObjectId,OpenMode.ForRead)
                    If (TypeOf TheObj Is Viewport)
                        If acVp Is Nothing Then
                            acVp = DirectCast(TheObj, Viewport)
                        Else
                            '// more than one vp selected
                            CMain.AlertUser(TX.ERR_OBJTY)
                            GoTo HaveUserSelect
                        End If
                    End If
                Next
                If (acVp Is Nothing) Then
                    CMain.AlertUser(TX.ERR_OBJTY)
                    GoTo HaveUserSelect
                End If
              '  Else
              '  HaveUserSelect:

    		        'peo = New PromptEntityOptions()
    		        'peo.SetRejectMessage(vbLf & CMain._T(TX.ERR_OBJTY) & "!")
    		        'peo.AddAllowedClass(GetType(Viewport), False)
    		        'Dim per As PromptEntityResult = acDoc.Editor.GetEntity(peo)
    		        'If per.Status <> PromptStatus.OK Then Exit Try
    		        'acVp = DirectCast( _
    		        '    acTrans.GetObject(per.ObjectId, OpenMode.ForRead), _
              '          Viewport)
              '  End If
              '  '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


                '// --------------------------------------------------
                '// get numerical elements related to this vp
                Dim viewMat As Matrix3d
			    Using vpView As Autodesk.AutoCAD.GraphicsSystem.View = _
                    New Autodesk.AutoCAD.GraphicsSystem.View

                    Dim gsm As Autodesk.AutoCAD.GraphicsSystem.Manager = _
                        Application.DocumentManager.MdiActiveDocument.GraphicsManager
			        Pt_MaxW = New Point3d( _
                        acVp.CenterPoint.X + acVp.Width / 2, _
                        acVp.CenterPoint.Y + acVp.Height / 2, 0)
			        Pt_MinW = New Point3d( _
                        acVp.CenterPoint.X - acVp.Width / 2, _
                        acVp.CenterPoint.Y - acVp.Height / 2, 0)

			        gsm.SetViewFromViewport(vpView, acVp.Number)
			        viewMat = vpView.ViewingMatrix.Inverse()
			        gsm.SetViewFromViewport(vpView, acVp.Number)
                End Using


		        Pt_MinW = Pt_MinW.Subtract(acVp.CenterPoint.GetAsVector)
		        Pt_MinW = Pt_MinW.MultiplyBy(1 / acVp.CustomScale)
		        Pt_MinW = Pt_MinW.TransformBy(viewMat)
		        Pt_MaxW = Pt_MaxW.Subtract(acVp.CenterPoint.GetAsVector)
		        Pt_MaxW = Pt_MaxW.MultiplyBy(1 / acVp.CustomScale)
		        Pt_MaxW = Pt_MaxW.TransformBy(viewMat)

			    scl = (Pt_MaxW.X - Pt_MinW.X) / acVp.Width

			    Dim SpcDstP_IN as Double = lcStg.P.IN_dst / scl
			    Dim SpcDstP_BR as Double = lcStg.P.BR_dst / scl

                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                '// --------------------------------------------------
                '// inside the viewport
                '// --------------------------------------------------
                
                ' start point in world coordinates
                PtSt_W = New Point3d( _
				    ((CLng(Pt_MinW.X + lcStg.P.IN_csz*scl) \ _
				      CLng(lcStg.P.IN_dst)) + 1) * _
				        lcStg.P.IN_dst, _
				    ((CLng(Pt_MinW.Y + lcStg.P.IN_csz*scl) \ _
				      CLng(lcStg.P.IN_dst)) + 1) * _
				        lcStg.P.IN_dst, _
				    0)
                ' start point in paper coordinates
                PtSt_P = New Point3d( _
                    acVp.CenterPoint.X - acVp.Width / 2 + _
                        (PtSt_W.X - Pt_MinW.X) / scl, _
                    acVp.CenterPoint.Y - acVp.Height / 2 + _
                        (PtSt_W.Y - Pt_MinW.Y) / scl, _
                    0)

                '// --------------------------------------------------
                '// loop inside
                dCrtE_P = PtSt_P.X
                dCrtE_M = PtSt_W.X


                Do Until dCrtE_P > (acVp.CenterPoint.X + acVp.Width/2 - lcStg.P.IN_csz) 
                    dCrtN_P = PtSt_P.Y
                    dCrtN_M = PtSt_W.Y
                    Do Until dCrtN_P > (acVp.CenterPoint.Y + acVp.Height/2 - lcStg.P.IN_csz)
                        If lcStg.P.IN_mrk Then
                            '// we have position where we have to place a marker at
                            acLine = New Line( _
                                New Point3d(dCrtE_P - lcStg.P.IN_csz / 2, dCrtN_P, 0), _
                                New Point3d(dCrtE_P + lcStg.P.IN_csz / 2, dCrtN_P, 0))
                            acLine.SetDatabaseDefaults(acDoc.Database)
						    acBlkModel.AppendEntity(acLine)
						    acTrans.AddNewlyCreatedDBObject(acLine, True)

                            acLine = New Line( _
                                New Point3d(dCrtE_P, dCrtN_P - lcStg.P.IN_csz / 2, 0), _
                                New Point3d(dCrtE_P, dCrtN_P + lcStg.P.IN_csz / 2, 0))
                            acLine.SetDatabaseDefaults(acDoc.Database)
						    acBlkModel.AppendEntity(acLine)
						    acTrans.AddNewlyCreatedDBObject(acLine, True)
                        End If
                        
                        If lcStg.P.IN_txt Then

                            '// Horizontal; the east
                            acText = New DBText()
							acText.SetDatabaseDefaults()
							acText.Position = New Point3d( _
                                dCrtE_P + lcStg.P.IN_txh / 2, _
                                dCrtN_P + lcStg.P.IN_txh / 2, _
                                0)
							acText.Height = lcStg.P.IN_txh
							acText.TextString = Format(dCrtE_M, "0")
							acText.SetDatabaseDefaults()
							acBlkModel.AppendEntity(acText)
							acTrans.AddNewlyCreatedDBObject(acText, True)

                            '// Vertical; the north
                            acText = New DBText()
							acText.SetDatabaseDefaults()
							acText.Position = New Point3d( _
                                dCrtE_P - lcStg.P.IN_txh / 2, _
                                dCrtN_P + lcStg.P.IN_txh / 2, _
                                0)
							acText.Height = lcStg.P.IN_txh
							acText.TextString = Format(dCrtN_M, "0")
							acText.SetDatabaseDefaults()
                            acText.Rotation = System.Math.PI / 2
							acBlkModel.AppendEntity(acText)
							acTrans.AddNewlyCreatedDBObject(acText, True)

                        End If
                        PctCnt = PctCnt + 1
                        dCrtN_M = dCrtN_M + lcStg.P.IN_dst
                        dCrtN_P = dCrtN_P + SpcDstP_IN
                    Loop' Until dCrtN_P > (acVp.CenterPoint.Y + acVp.Height/2 - lcStg.P.IN_csz)
                    dCrtE_M = dCrtE_M + lcStg.P.IN_dst
                    dCrtE_P = dCrtE_P + SpcDstP_IN
                Loop' Until dCrtE_P > (acVp.CenterPoint.X + acVp.Width/2 - lcStg.P.IN_csz)
                CMain.InformUser(String.Format(CMain._T(TX.GRIDRSLT_I), PctCnt))
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


                '// --------------------------------------------------
                '// border of the viewport
                '// --------------------------------------------------

                ' start point in world coordinates
                PtSt_W = New Point3d( _
				    ((CLng(Pt_MinW.X + lcStg.P.BR_csz*scl) \ _
				      CLng(lcStg.P.BR_dst)) + 1) * _
				        lcStg.P.BR_dst, _
				    ((CLng(Pt_MinW.Y + lcStg.P.BR_csz*scl) \ _
				      CLng(lcStg.P.BR_dst)) + 1) * _
				        lcStg.P.BR_dst, _
				    0)
                ' start point in paper coordinates
                PtSt_P = New Point3d( _
                    acVp.CenterPoint.X - acVp.Width / 2 + _
                        (PtSt_W.X - Pt_MinW.X) / scl, _
                    acVp.CenterPoint.Y - acVp.Height / 2 + _
                        (PtSt_W.Y - Pt_MinW.Y) / scl, _
                    0)

                '// --------------------------------------------------
                '// loop inside
                PctCnt = 0
                '// loop on East (X advances)
                dCrtE_P = PtSt_P.X
                dCrtE_M = PtSt_W.X
                Do Until dCrtE_P > (acVp.CenterPoint.X + acVp.Width/2 - lcStg.P.IN_csz)
                    If lcStg.P.BR_mrk Then

                        acLine = New Line( _
                            New Point3d(dCrtE_P, _
                                acVp.CenterPoint.Y - acVp.Height / 2, 0), _
                            New Point3d(dCrtE_P, _
                                acVp.CenterPoint.Y - acVp.Height / 2 + _
                                    lcStg.P.BR_csz / 2, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                        acLine = New Line( _
                            New Point3d(dCrtE_P, _
                                acVp.CenterPoint.Y + acVp.Height / 2, 0), _
                            New Point3d(dCrtE_P, _
                                acVp.CenterPoint.Y + acVp.Height / 2 - _
                                    lcStg.P.BR_csz / 2, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                    End If
                    If lcStg.P.BR_txt Then
                        '// top
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                                dCrtE_P + _
                                    lcStg.P.BR_txh / 2,  _
                                acVp.CenterPoint.Y + acVp.Height / 2 - _
                                    3 * lcStg.P.BR_txh / 2, _
                                0)
						acText.Height = lcStg.P.BR_txh
						acText.TextString = Format(dCrtE_M, "0")
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)

                        '// bottom
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                                dCrtE_P + _
                                    lcStg.P.BR_txh / 2,  _
                                acVp.CenterPoint.Y - acVp.Height / 2 + _
                                    lcStg.P.BR_txh / 2, _
                                0)
						acText.Height = lcStg.P.BR_txh
						acText.TextString = Format(dCrtE_M, "0")
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)
                    End If
                    PctCnt = PctCnt + 1
                    dCrtE_M = dCrtE_M + lcStg.P.BR_dst
                    dCrtE_P = dCrtE_P + SpcDstP_BR
                Loop' Until dCrtE_P > (acVp.CenterPoint.X + acVp.Width/2 - lcStg.P.IN_csz)

                '// loop on North (Y advances)
                dCrtN_P = PtSt_P.Y
                dCrtN_M = PtSt_W.Y
                Do Until dCrtN_P > (acVp.CenterPoint.Y + acVp.Height/2 - lcStg.P.IN_csz)
                    If lcStg.P.BR_mrk Then
                        acLine = New Line( _
                            New Point3d(acVp.CenterPoint.X - _
                                        acVp.Width / 2, _
                                dCrtN_P, 0), _
                            New Point3d(acVp.CenterPoint.X - _
                                acVp.Width / 2 + lcStg.P.BR_csz / 2, _
                                dCrtN_P, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)

                        acLine = New Line( _
                            New Point3d(acVp.CenterPoint.X + _
                                        acVp.Width / 2, _
                                dCrtN_P, 0), _
                            New Point3d(acVp.CenterPoint.X + _
                                acVp.Width / 2 - lcStg.P.BR_csz / 2, _
                                dCrtN_P, 0))
                        acLine.SetDatabaseDefaults(acDoc.Database)
					    acBlkModel.AppendEntity(acLine)
					    acTrans.AddNewlyCreatedDBObject(acLine, True)
                    End If
                    If lcStg.P.BR_txt Then
                        '// left
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                                acVp.CenterPoint.X - acVp.Width / 2 + _
                                    3 * lcStg.P.BR_txh / 2, _
                                dCrtN_P + _
                                    lcStg.P.BR_txh / 2,  _
                                0)
						acText.Height = lcStg.P.BR_txh
						acText.TextString = Format(dCrtN_M, "0")
                        acText.Rotation = System.Math.PI / 2
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)

                        '// right
                        acText = New DBText()
						acText.SetDatabaseDefaults()
						acText.Position = New Point3d( _
                                acVp.CenterPoint.X + acVp.Width / 2 - _
                                    lcStg.P.BR_txh / 2, _
                                dCrtN_P + _
                                    lcStg.P.BR_txh / 2,  _
                                0)
						acText.Height = lcStg.P.BR_txh
						acText.TextString = Format(dCrtN_M, "0")
                        acText.Rotation = System.Math.PI / 2
						acBlkModel.AppendEntity(acText)
						acTrans.AddNewlyCreatedDBObject(acText, True)
                    End If
                    PctCnt = PctCnt + 1
                    dCrtN_M = dCrtN_M + lcStg.P.BR_dst
                    dCrtN_P = dCrtN_P + SpcDstP_BR
                Loop' Until dCrtN_P > (acVp.CenterPoint.Y + acVp.Height/2 - lcStg.P.IN_csz)

                CMain.InformUser(String.Format(CMain._T(TX.GRIDRSLT_B), PctCnt))

                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                acTrans.Commit()
                bRet = True
                Exit Try
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
            End Try
        End Using '// acTrans
        End Using '// acLock

        Return bRet

    End Function
    '// ===================================================================== '


#End Region


#Region "C O M M A N D S"


    '// --------------------------------------------------------------------- '
	<CommandMethod(sCMD_SETTINGS)> _
	Public Sub Show_Settings()
		Try

            Dim StgD As CST_Grid = New CST_Grid
            Application.ShowModelessDialog(StgD)
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	<CommandMethod(sCMD_MODEL)> _
	Public Sub DoModel()
		Try
            Do_Model_Grid(Stgs)
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	<CommandMethod(sCMD_PAPER, CommandFlags.UsePickSet)> _
	Public Sub DoPaper()
		Try
            Do_Paper_Grid(Stgs)
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	<CommandMethod(sCMD_MODELOPTS)> _
	Public Sub DoModelOpts()
		Try

            Dim StgD As CST_Grid = New CST_Grid(CST_Grid.MODE_MODEL)
            Application.ShowModelessDialog(StgD)
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
	End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
	<CommandMethod(sCMD_PAPEROPTS, CommandFlags.UsePickSet)> _
	Public Sub DoPaperOpts()
		Try

            Dim StgD As CST_Grid = New CST_Grid(CST_Grid.MODE_PAPER)
            Application.ShowModelessDialog(StgD)
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()

	End Sub
    '// ===================================================================== '


#End Region




End Class
'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '


